#!/usr/bin/python
#coding=utf-8
import socket,struct
import math

def get_ip_duan(cidr_str):
    if type(cidr_str) != type(''):
        return None
    
    a= cidr_str.split('/')
    ip = a[0]
    wei = int(a[1])
    if wei >32 or wei < 0:
        return None
    #print ip,wei
    if 0 == wei:
        ip_start = '0.0.0.0'
        ip_end = '255.255.255.255'
    elif 32== wei:
        ip_start = ip
        ip_end = ip
    else:
        ip_int = struct.unpack('!I',socket.inet_aton(ip))[0];
        #ip = socket.inet_ntoa(struct.pack('!I',ip_int))
        ip_start_int = (ip_int >>(32-wei))<<(32-wei)
        ip_start = socket.inet_ntoa(struct.pack('!I',ip_start_int))
        
        ip_end_int = ip_start_int+ ((pow(2, 32)-1) >>wei)
        ip_end = socket.inet_ntoa(struct.pack('!I',ip_end_int))
        
    ip_duan =  [ip_start,ip_end]
    return ip_duan

def get_ip_cidr(start_ip_str,end_ip_str):
    ip_duan =[]

    if (type(start_ip_str) != type('')) or (type(end_ip_str) != type('')):
        return None
    #print start_ip_str,end_ip_str
    try:
        start_ip = long(struct.unpack("!I",socket.inet_aton(start_ip_str))[0])
        end_ip = long(struct.unpack("!I",socket.inet_aton(end_ip_str))[0])
    except Exception,e:
        #print e
        return None
    
       
    while(start_ip <=end_ip):
        if start_ip == end_ip:
            #ip_str = socket.inet_ntoa(struct.pack('I',socket.htonl(start_ip)))
            ip_cidr = end_ip_str+'/'+str(32-0)
            
            ip_duan.append(end_ip_str+'\t'+end_ip_str+'\t'+ip_cidr)
            break
        max_maxwei = int(math.log(end_ip - start_ip+1)/math.log(2))
        max_wei =1
        while (start_ip == (start_ip >> max_wei << max_wei)) and (max_wei <= max_maxwei):
            max_wei =max_wei +1
        max_wei = max_wei-1
        
    
        b_ip = socket.inet_ntoa(struct.pack('I',socket.htonl(start_ip)))
        e_ip = socket.inet_ntoa(struct.pack('I',socket.htonl(start_ip+pow(2,max_wei)-1)))
        
        ip_cidr = b_ip+'/'+str(32-max_wei)
        
        ip_duan.append(b_ip+'\t'+e_ip+'\t'+ip_cidr)
        start_ip = start_ip+pow(2,max_wei)

    return ip_duan
    
def is_ip_in_cidr(ip,cidr_str):
    if type(ip) != type(''):
        return False
    if type(cidr_str) != type(''):
        return False
    try:
        ip_int = struct.unpack('!I',socket.inet_aton(ip))[0];
    except Exception,e:
        print "ip",e
        return False
    try:
        temp_aa = cidr_str.split('/')
        if len(temp_aa) != 2:
            return False
        wei = int(temp_aa[1])
        if wei >32 or wei < 0:
            return False
        ip_temp = struct.unpack('!I',socket.inet_aton(temp_aa[0]))[0]
        ip_net = ip_temp >>(32-wei) <<(32-wei)
        #print socket.inet_ntoa(struct.pack('I',socket.htonl(ip_net)))
    except Exception,e:
        print "cidr_str",e
        return False
    ip_net0 = ip_int >>(32-wei) <<(32-wei)
    if ip_net0 == ip_net:
        return True
    else:
        return False

def output_ips(first_ip_str,last_ip_str,file_name):
    if first_ip_str ==''or last_ip_str =='' or file_name =='':
        return
    try:
        first_int = struct.unpack("!I",socket.inet_aton(str(first_ip_str)))[0]
        end_int  = struct.unpack("!I",socket.inet_aton(str(last_ip_str)))[0]
    except Exception,e:
        print e
        return
    
    file = open(file_name,'a+')
    while first_int <=end_int:
        file.write(socket.inet_ntoa(struct.pack('!I',first_int)))
        file.write('\n')
        first_int +=1
    file.flush()
    file.close()

def domain_resolve(domain):
    ips = []
    try:
        results=socket.getaddrinfo(domain,None)
        for result in results:
            ips.append(result[4][0])
    except Exception,e:
        print e
    return ips

if __name__ == '__main__':
    print get_ip_duan('127.0.0.1/24')
    print get_ip_cidr('127.0.0.1','127.1.2.255')
    print ip_ip_in_cidr('1.2.2.1', '1.0.0.1/8')
    output_ips("127.0.0.1","127.0.10.255","test.txt")
    print domain_resolve("www.baidu.com")